$(function () {
    // 获取拍品
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    var result = Model.user.myGoods();
    if (!sessionStorage.getItem('hmdj_stopError')) {
        sessionStorage.setItem('hmdj_stopError', true);
    } else {
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
    }
    for (var i = 0; i < result.data.length; i++) {
        var item = '<div class="pruduct-manager" data-id="' + result.data[i].goodsId + '"><div class="weui-cell"><img class="portrait-img" src="' + result.data[i].photo + '"><div class="weui-cell__hd"><h3 class="text-1-6">' + result.data[i].content + '</h3><p class="patron-price">￥' + result.data[i].topPrice + '起 ￥' + result.data[i].botPrice + '保</p>';
        switch (result.data[i].state) {
            case '下架':
                item += '<a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default change-state-btn" data-state="1" data-id="' + result.data[i].goodsId + '">重新上架</a><a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default del-product-btn" data-id="' + result.data[i].goodsId + '">删除</a><a href="edit_goods.html?gid=' + result.data[i].goodsId + '" class="weui-btn weui-btn_mini weui-btn_default edit-product-btn" data-id="' + result.data[i].goodsId + '">编辑商品</a>';
                break;
            case '上架':
                item += '<a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default change-state-btn" data-state="0" data-id="' + result.data[i].goodsId + '">下架</a>';
                break;
            case '售出':
                item += '<span href="javascript:;" data-id="' + result.data[i].goodsId + '">已售出</span>';
                break;
        }
        item += '</div></div></div>';
        $('.s-content').append(item);
    }
    loading.hide();
});

$(document).on('mousedown', '.change-state-btn', function () {
    var id = $(this).attr('data-id'),
        state = $(this).attr('data-state'),
        title = '';
    state == 0 ? title = '下架' : title = '重新上架';
    var confirms = weui.confirm(
        "真的要" + title + "吗？",
        function () {
            // 上下架管理
            var result = Model.user.putOffGoods({
                goodsId: id,
                type: state
            });
            if (result.code) {
                alert(result.message);
                location.reload();
                return false;
            }
            location.reload();
        },
        function () {}
    );
});

$(document).on('mousedown', '.del-product-btn', function () {
    var id = $(this).attr('data-id');
    var confirms = weui.confirm(
        "真的要删除该拍品吗？",
        function () {
            // 删除拍品
            var result = Model.user.delGoods({
                goodsId: id,
            });
            if (result.code) {
                alert(result.message);
                location.reload();
                return false;
            }
            location.reload();
        },
        function () { }
    );
});


// 页面跳转
$(document).on('click', '.pruduct-manager', function () {
    location.href = 'goods_detail_seller.html?gid=' + $(this).attr('data-id');
});